<template>
  <div>
    <el-card class="box-card">
      <el-row>
        <el-col :span="20">
      <el-tabs class="direct" v-model="activeName" @tab-click="handleClick">
        <el-tab-pane @click="plan" label="巡检计划" name="1"></el-tab-pane>
        <el-tab-pane @click="message" label="巡检工单" name="2"></el-tab-pane>
      </el-tabs>
        </el-col>
      <el-col :span="20" class="btn1">
        <el-button type="primary" @click="createPlan" v-show="planView">创建计划</el-button>
        <el-button type="primary" icon="el-icon-search"/>
      </el-col>

      </el-row>
    </el-card>

    <el-card :inline="true" v-show="planView" class="box-card">
      <el-col :span="11">
        <div id="chart" style="width: 600px; height: 300px;"/>
      </el-col>
      <el-col :span="11" class="chart1">
        <div>
          <el-row :gutter="12">
            <el-col :span="4">
              <div>
                <el-statistic title="全部计划">
                  <template slot="formatter">
                    {{ numArr[0] }}
                  </template>

                </el-statistic>
              </div>
            </el-col>
            <el-col :span="4">
              <div>
                <el-statistic title="计划类型A">
                  <template slot="formatter">
                    {{ numArr[1] }}
                  </template>
                </el-statistic>
              </div>
            </el-col>
            <el-col :span="4">
              <div>
                <el-statistic title="计划类型B">
                  <template slot="formatter">
                    {{ numArr[2] }}
                  </template>
                </el-statistic>
              </div>
            </el-col>
            <el-col :span="4">
              <div>
                <el-statistic title="计划类型C">
                  <template slot="formatter">
                    {{ numArr[3] }}
                  </template>
                </el-statistic>
              </div>
            </el-col>
            <el-col :span="4">
              <div>
                <el-statistic title="计划类型D">
                  <template slot="formatter">
                    {{ numArr[4] }}
                  </template>
                </el-statistic>
              </div>
            </el-col>
          </el-row>
        </div>
        <div style="margin-top: 50px">
          <el-row :gutter="12">
            <el-col :span="4">
              <div>
                <el-statistic title="占比">
                  <template slot="formatter">
                    {{ numArr[0] / numArr[0] * 100 }}
                    <i slot="suffer">%</i>
                  </template>
                </el-statistic>
              </div>
            </el-col>
            <el-col :span="4">
              <div>
                <el-statistic title="占比">
                  <template slot="formatter">
                    {{ Number(numArr[1] / numArr[0] * 100).toFixed(2) }}
                    <i slot="suffer">%</i>
                  </template>
                </el-statistic>
              </div>
            </el-col>
            <el-col :span="4">
              <div>
                <el-statistic title="占比">
                  <template slot="formatter">
                    {{ Number(numArr[2] / numArr[0] * 100).toFixed(2) }}
                    <i slot="suffer">%</i>
                  </template>
                </el-statistic>
              </div>
            </el-col>
            <el-col :span="4">
              <div>
                <el-statistic title="占比">
                  <template slot="formatter">
                    {{ Number(numArr[3] / numArr[0] * 100).toFixed(2) }}
                    <i slot="suffer">%</i>
                  </template>
                </el-statistic>
              </div>
            </el-col>
            <el-col :span="4">
              <div>
                <el-statistic title="占比">
                  <template slot="formatter">
                    {{ Number(numArr[4] / numArr[0] * 100).toFixed(2) }}
                    <i slot="suffer">%</i>
                  </template>
                </el-statistic>
              </div>
            </el-col>
          </el-row>
        </div>
      </el-col>
    </el-card>

    <el-card class="box-card" v-show="planView">
      <el-table :data="patrolList" style="width: 100%">
        <el-table-column label="序号" width="100">
          <template scope="scope">
            <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="planName" label="计划名称" width="160"></el-table-column>
        <el-table-column prop="patrol" label="巡检类型" width="130">
          <template scope="scope">
            <span>{{ scope.row.patrol == 1 ? '自动巡检' : '人工巡检' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="username" label="创建人" width="160"></el-table-column>
        <el-table-column prop="phone" label="联系方式" width="180"></el-table-column>
        <el-table-column prop="createDate" label="创建日期" width="180"></el-table-column>
        <el-table-column prop="enableStatus" label="启用情况" width="130">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enableStatus"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="2"
            >
            </el-switch>
          </template>
          >
        </el-table-column>
        <el-table-column label='操 作' width="200">
          <el-col :span="11">
            <el-button type="warning" size="mini">查看详情</el-button>
          </el-col>
          <el-col :span="11">
            <el-button type="primary" size="mini">巡检记录</el-button>
          </el-col>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-left: 800px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 20, 30]"
          :pageSize="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>

    </el-card>

    <el-dialog title="创建计划" :visible.sync="dialogFormVisible" center width="500px">
      <el-form :model="form" ref="formInline">
        <el-form-item label="计划名称" prop="planName" :label-width="formLabelWidth">
          <el-input v-model="form.planName" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="巡检类型" prop="patrol" :label-width="formLabelWidth">
          <el-select v-model="form.patrol" placeholder="请选择巡检类型">
            <el-option label="自动巡检" value=0></el-option>
            <el-option label="人工巡检" value=1></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="计划类型" prop="planType" :label-width="formLabelWidth">
          <el-select v-model="form.planType" placeholder="请选择计划类型">
            <el-option label="计划类型A" value="A"></el-option>
            <el-option label="计划类型B" value="B"></el-option>
            <el-option label="计划类型C" value="C"></el-option>
            <el-option label="计划类型D" value="D"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { selectPage, typeCount, insertInfo } from '@/api/patrol'
import * as echarts from 'echarts'

export default {
  name: 'MyChart',
  mounted() {
    this.initChart()
  },
  data() {
    return {
      form: {
        planName: '',
        patrol: '',
        planType: ''
      },
      planView: true,
      numArr: [],
      dialogFormVisible: false,
      activeIndex: '1',
      patrolList: [],
      pageNum: 1,
      pageSize: 5,
      formLabelWidth: '120px',
      total: 1,
      activeName: '1'
    }
  },
  methods: {
    featchData() {
      let params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }
      selectPage(params).then(response => {
        this.patrolList = response.data.records
        this.total = response.data.total
      })
    },
    createPlan() {
      this.dialogFormVisible = true
      this.$refs.formInline.resetFields()
    },
    onSubmit() {
      this.dialogFormVisible = false
      insertInfo(this.form).then(res => {
        if (res.code == 20000) {
          this.$message({
            message: '创建成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '创建失败',
            type: 'warning'
          })
        }
      })
    },
    /* 图标数据 */
    initChart() {
      // 2初始化
      const chart = echarts.init(document.getElementById('chart'))
      typeCount().then(res => {
        let retData = res.data
        this.numArr = retData.numbers
        console.log(this.numArr)
        var planType = new Array()
        planType.push('全部计划')
        // 3 配置
        const option = {
          title: { text: '巡检计划' },
          tooltip: { trigger: 'axis' },
          xAxis: { type: 'category', data: retData.type },
          yAxis: { type: 'value' },
          series: [{ name: '任务量', colorBy: 'data', type: 'bar', data: retData.numbers, barWidth: '30%' }]
        }
        // 4使用图表配置
        chart.setOption(option)

      })
    },
    /*分页*/
    handleSizeChange(val) {
      this.pageSize = val
      console.log(this.pageSize)
      this.featchData()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.featchData()
    },
    handleClick() {
      console.log(this.activeName)
      if (this.activeName == '2') {
        this.planView = false
      } else {
        this.planView = true
      }
    },
    plan() {
      this.planView = true
    },
    message() {

    }
  },
  created() {
    this.featchData()
  }
}
</script>

<style>
.box-card {
  width: 97%;
  margin: 15px auto auto;
}
.direct{
  width: 900px;
}
.btn1 {
  position: relative;
  left: 80%;
  top: -20%;
}


.chart1 {
  margin-top: 75px;
}


</style>
